package com.movitech.mobile.cz.base.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.movitech.mobile.cz.base.bizconfig.service.SSuppliersCategoryService;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.common.util.NumberUtils;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.entity.*;
import com.movitech.mobile.cz.base.mapper.SupplierReportMapper;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by robie.cao on 2018/5/18.
 */
@Service("supplierReportService")
@ConfigurationProperties(
        prefix = "supplier-report-classify"
)
@Transactional
public class SupplierReportService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private String single;
    private List<String> groups;

    @Autowired
    private SupplierReportMapper supplierReportMapper;
    @Autowired
    private SSuppliersCategoryService sSuppliersCategoryService;

    public List<QualifedSupplierRspVO> qualifiedSupplierCount(QualifedSupplierReqVO qualifedSupplierReqVO){
        String[] l2 =single.split(",");
        List<QualifedSupplierRspVO> ls=supplierReportMapper.getSupplierCategoryByClassify(l2);
        for(QualifedSupplierRspVO qualifedSupplierRspVO:ls){
//            List<String> cs = sSuppliersCategoryService.selectShowIdsByParentId(qualifedSupplierRspVO.getCategoryId());
//            if(CollectionUtils.isEmpty(cs))
//                cs = Lists.newArrayListWithCapacity(1);
//            cs.add(qualifedSupplierRspVO.getCategoryId());
            List<String> cs = getClassifyInclusionSubCategory(qualifedSupplierRspVO.getCategoryId());
            qualifedSupplierRspVO.setClassifys(cs.toArray(new String[cs.size()]));

            qualifedSupplierRspVO.setRegionalCompanyId(qualifedSupplierReqVO.getRegionalCompanyId());
            //现有供应商总数量
            //Integer supplierTotalCount=supplierReportMapper.qualifiedSupplierCount(qualifedSupplierRspVO);
            Integer supplierTotalCount=supplierReportMapper.qualifiedSupplierCountIncludeClassify(qualifedSupplierRspVO);
            qualifedSupplierRspVO.setStoredEndTime(qualifedSupplierReqVO.getStoredEndTime());
            qualifedSupplierRspVO.setStoredStartTime(qualifedSupplierReqVO.getStoredStartTime());
            //新增供应商数量
            //Integer supplierAddCount=supplierReportMapper.qualifiedSupplierCount(qualifedSupplierRspVO);
            Integer supplierAddCount=supplierReportMapper.qualifiedSupplierCountIncludeClassify(qualifedSupplierRspVO);

            qualifedSupplierRspVO.setSupplierAddCount(supplierAddCount);
            qualifedSupplierRspVO.setSupplierTotalCount(supplierTotalCount);
        }

        for(String group :groups){
            String[] gs = group.split("-");
            List<String> c3s = getClassifyInclusionSubCategory(gs[0]);
            QualifedSupplierRspVO qualifedSupplierRspVO = new QualifedSupplierRspVO();
            qualifedSupplierRspVO.setCategoryId(gs[0]);
            qualifedSupplierRspVO.setCategoryName(gs[1]);
            qualifedSupplierRspVO.setClassifys(c3s.toArray(new String[c3s.size()]));
            qualifedSupplierRspVO.setRegionalCompanyId(qualifedSupplierReqVO.getRegionalCompanyId());
            Integer supplierTotalCount=supplierReportMapper.qualifiedSupplierCountIncludeClassify(qualifedSupplierRspVO);
            qualifedSupplierRspVO.setStoredEndTime(qualifedSupplierReqVO.getStoredEndTime());
            qualifedSupplierRspVO.setStoredStartTime(qualifedSupplierReqVO.getStoredStartTime());
            Integer supplierAddCount=supplierReportMapper.qualifiedSupplierCountIncludeClassify(qualifedSupplierRspVO);

            qualifedSupplierRspVO.setSupplierAddCount(supplierAddCount);
            qualifedSupplierRspVO.setSupplierTotalCount(supplierTotalCount);
            qualifedSupplierRspVO.setClassifys(null);

            ls.add(qualifedSupplierRspVO);
        }
        return ls;
    }



    public List<ExtSSupplierRcRelateRsp> qualifiedSupplierDtl(QualifedSupplierReqVO qualifedSupplierReqVO){
        List<String> cs = getClassifyInclusionSubCategory(qualifedSupplierReqVO.getCategoryId());
        qualifedSupplierReqVO.setClassifys(cs.toArray(new String[cs.size()]));
        List<ExtSSupplierRcRelateRsp> ls =supplierReportMapper.qualifiedSupplierDtl(qualifedSupplierReqVO);

             for(ExtSSupplierRcRelateRsp obj:ls){
                 String supplierCategoryActualIds = obj.getSupplierCategoryActualIds();
                 if (supplierCategoryActualIds != null && supplierCategoryActualIds.length() > 0) {
                     String[] supplierCategoryIdArr = supplierCategoryActualIds.split(",");
                     String supplierCategoryNames = supplierReportMapper.getSupplierCategoryNames(supplierCategoryIdArr);
                     obj.setSupplierCategoryActualNames(supplierCategoryNames);
                 }

                 if(obj.getSupplierSource()!=null&&obj.getSupplierSource()==1){
                     obj.setSupplierSourceName("新增");
                 }else if(obj.getSupplierSource()!=null&&obj.getSupplierSource()==2){
                     obj.setSupplierSourceName("引入");
                 }
             }
        return ls;
    }

    public List<List<StarLevelCountRspVO>> levelCount(StarLevelCountReqVO starLevelCountReqVO) {
        Map<String,String> classifyMap = getFixedClassifys();

        List<List<StarLevelCountRspVO>> lists = Lists.newArrayList();
        Integer year = starLevelCountReqVO.getRankYear();
        if(year==null){
            year = Integer.valueOf(DateUtils.format(DateUtils.getNow(),"yyyy"));
        }

        Set<String> classList = Sets.newHashSet();
        if(StringUtils.isEmpty(starLevelCountReqVO.getClassify())){
            classList = classifyMap.keySet();
        }else{
            classList.add(starLevelCountReqVO.getClassify());
        }
        for(String c:classList){
            List<String> classifys = getClassifyInclusionSubCategory(c);
            String classifyName = classifyMap.get(c);
            List<StarLevelCountRspVO> list = supplierReportMapper.levelCount(year,classifys);
            if(CollectionUtils.isNotEmpty(list)){
                Double sum = 0d;
                for(StarLevelCountRspVO starLevelCountRspVO:list){
                    sum+=starLevelCountRspVO.getCount();
                    starLevelCountRspVO.setClassifyName(classifyName);
                }
                for(StarLevelCountRspVO starLevelCountRspVO:list){
                    starLevelCountRspVO.setProportion(NumberUtils.seekercentage(starLevelCountRspVO.getCount()/sum));
                }
                lists.add(list);
            }
        }


        return lists;
    }

    public List<List<StarLevelCountRspVO>> levelCountNew(StarLevelCountReqVO starLevelCountReqVO) {
        Map<String,String> classifyMap = getFixedClassifys();

        List<List<StarLevelCountRspVO>> lists = Lists.newArrayList();

        Set<String> classList = Sets.newHashSet();
        if(StringUtils.isEmpty(starLevelCountReqVO.getClassify())){
            classList = classifyMap.keySet();
        }else{
            classList.add(starLevelCountReqVO.getClassify());
        }
        for(String c:classList){
            List<String> classifys = getClassifyInclusionSubCategory(c);
            String classifyName = classifyMap.get(c);
            List<StarLevelCountRspVO> list = supplierReportMapper.levelCountNew(classifys,starLevelCountReqVO.getRegionalCompanyId());
            if(CollectionUtils.isNotEmpty(list)){
                Double sum = 0d;
                for(StarLevelCountRspVO starLevelCountRspVO:list){
                    sum+=starLevelCountRspVO.getCount();
                    starLevelCountRspVO.setClassifyName(classifyName);
                }
                for(StarLevelCountRspVO starLevelCountRspVO:list){
                    starLevelCountRspVO.setProportion(NumberUtils.seekercentage(starLevelCountRspVO.getCount()/sum));
                }
                lists.add(list);
            }
        }


        return lists;
    }

    public Map<String,String> getFixedClassifys(){
        String[] ss =single.split(",");
        Map<String,String> map = Maps.newHashMap();
        List<QualifedSupplierRspVO> ls=supplierReportMapper.getSupplierCategoryByClassify(ss);
        for(QualifedSupplierRspVO qualifedSupplierRspVO:ls){
            map.put(qualifedSupplierRspVO.getCategoryId(),qualifedSupplierRspVO.getCategoryName());
        }
        for(String group:groups){
            String[] gs = group.split("-");
            map.put(gs[0],gs[1]);
        }
        return map;
    }


    public List<String> getClassifyInclusionSubCategory(String caId){
        List<String> cs = null;
        if(!caId.startsWith("G")){
            cs = sSuppliersCategoryService.selectShowIdsByParentId(caId);
            if(CollectionUtils.isEmpty(cs))
                cs = Lists.newArrayListWithCapacity(1);
            cs.add(caId);
        }else{
            for(String group :groups){
                String[] gs = group.split("-");
                if(gs[0].equals(caId)){
                    String[] l3 =gs[2].split(",");
                    List<String> c3s = Lists.newArrayList();
                    for(String s:l3){
                        c3s.add(s);
                        cs = sSuppliersCategoryService.selectShowIdsByParentId(s);
                        if(CollectionUtils.isNotEmpty(cs))
                            c3s.addAll(cs);
                    }
                    cs = c3s;
                }
            }
        }
        return cs;
    }



    public String getSingle() {
        return single;
    }

    public SupplierReportService setSingle(String single) {
        this.single = single;
        return this;
    }

    public List<String> getGroups() {
        return groups;
    }

    public SupplierReportService setGroups(List<String> groups) {
        this.groups = groups;
        return this;
    }
}
